package rsj.api.account;

import com.rlyy.aibase.utils.base.Box;
import common.checkgroup.AddGroup;
import common.util.Assert;
import common.util.BoxUtil;
import common.util.validator.ParameterConversionCheck;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import rsj.api.account.entity.*;
import rsj.log.ModuleEnum;
import rsj.log.OperationLog;
import rsj.log.OperationTypeEnum;
import rsj.service.account.IAccountProductService;
import rsj.service.account.IAccountService;

import java.util.List;

/**
 * 账号
 *
 * @author yuanqinglong
 * @since 2021/7/14 13:41
 */
@RestController
public class AccountController {


    @Autowired
    private IAccountService accountServiceImpl;


    @Autowired
    private IAccountProductService accountProductServiceImpl;

    /**
     * 账户列表框
     *
     * @param param 参数
     * @return {@link Box}
     */
    @RequestMapping(value = "rsj_account_list")
    public Box accountList(Box param) {
        AccountQueryParam accountQueryParam = ParameterConversionCheck.parameterConversionCheckFastFail(param, AccountQueryParam.class);
        List<AccountTable> accountTables = accountServiceImpl.accountList(accountQueryParam);
        int total = accountServiceImpl.accountCount(accountQueryParam);
        return BoxUtil.successBoxToTableData(param, accountTables, total);
    }


    /**
     * 添加帐户列表
     *
     * @param param 参数
     * @return {@link Box}
     */
    @RequestMapping(value = "rsj_account_add")
    @OperationLog(description = "新增账号", logType = OperationTypeEnum.ADD, logModule = ModuleEnum.ACCOUNT)
    public Box addAccount(Box param) {
        AccountInformation createAccount = ParameterConversionCheck.parameterConversionCheckFastFail(param, AccountInformation.class, AddGroup.class);
        accountServiceImpl.addAccount(createAccount);
        return BoxUtil.successBox(param);
    }


    /**
     * 获得账户
     *
     * @param param 参数
     * @return {@link Box}
     */
    @RequestMapping(value = "rsj_account_get")
    public Box getAccount(Box param) {
        String orgId = param.getMapNode("data").getValue("org_id");
        Assert.isNotBlank(orgId, "机构id不可为空");
        AccountInformation accountInformation = accountServiceImpl.getAccount(orgId);
        return BoxUtil.successBoxToData(param, accountInformation);
    }


    /**
     * 修改账号
     *
     * @param param 参数
     * @return {@link Box}
     */
    @RequestMapping(value = "rsj_account_update")
    @OperationLog(description = "修改账号", logType = OperationTypeEnum.UPDATE, logModule = ModuleEnum.ACCOUNT)
    public Box updateAccount(Box param) {
        AccountInformation createAccount = ParameterConversionCheck.parameterConversionCheckFastFail(param, AccountInformation.class);
        accountServiceImpl.updateAccount(createAccount);
        return BoxUtil.successBox(param);
    }


    /**
     * 更新帐户状态
     *
     * @param param 参数
     * @return {@link Box}
     */
    @RequestMapping(value = "rsj_account_status_update")
    public Box updateAccountStatus(Box param) {
        AccountStatus accountStatus = ParameterConversionCheck.parameterConversionCheckFastFail(param, AccountStatus.class);
        accountServiceImpl.updateAccountStatus(accountStatus);
        return BoxUtil.successBox(param);
    }


    /**
     * 更新账户产品
     *
     * @param param 参数
     * @return {@link Box}
     */
    @RequestMapping(value = "rsj_account_product_update")
    public Box updateAccountProduct(Box param) {
        UpdateAccountProduct updateAccount = ParameterConversionCheck.parameterConversionCheckFastFail(param, UpdateAccountProduct.class);
        accountProductServiceImpl.addAccountProduct(updateAccount.getAccountProducts(), updateAccount.getOrgId());
        return BoxUtil.successBox(param);
    }

    /**
     * 检查删除
     *
     * @param param 参数
     * @return {@link Box}
     */
    @RequestMapping(value = "rsj_product_delete_check")
    public Box checkDelete(Box param) {
        DeleteAccountParam deleteAccountParam = ParameterConversionCheck.parameterConversionCheckFastFail(param, DeleteAccountParam.class);
        CheckDeleteResult checkDeleteResult = accountProductServiceImpl.checkDelete(deleteAccountParam);
        return BoxUtil.successBoxToData(param, checkDeleteResult);
    }


}
